package com.guyan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guyan.entity.Shipping;
import com.guyan.mapper.ShippingMapper;
import com.guyan.service.ShippingService;
import com.guyan.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ClassName： ShippingServiceImpl
 * Date： 2021/6/10 23:11
 *
 * @author 孤焰
 * @version 1.0
 **/

@Service
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    @Override
    public PageInfo shippingList(Integer page, Integer limit, Integer userId) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(shippingMapper.shippingList(userId));
        return pageInfo;
    }

    @Override
    public List<Shipping> shippingListNoPage(Integer userId) {
        return shippingMapper.shippingList(userId);
    }

    @Override
    public ResultVo addShipping(Shipping shipping) {
        Date date = new Date();
        shipping.setCreateTime(date);
        shipping.setUpdateTime(date);

        int row = shippingMapper.addShipping(shipping);
        return row > 0 ? ResultVo.success() : ResultVo.error(6000, shipping + "数据插入失败");
    }

    @Override
    public ResultVo batchDelShipping(Integer[] ids) {
        return shippingMapper.batchDelShipping(ids) > 0 ?
                ResultVo.success() : ResultVo.error(6000, "数据批量删除失败");
    }

    @Override
    public Shipping getShippingById(Integer id) {
        return shippingMapper.getShippingById(id);
    }

    @Override
    public ResultVo updateShippingById(Shipping shipping) {
        shipping.setUpdateTime(new Date());

        return shippingMapper.updateShippingById(shipping) > 0 ?
                ResultVo.success() : ResultVo.error(6000, shipping+"数据库层更新失败");
    }
}
